package com.wjb.xietong.server.taskdetail;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.wjb.xietong.server.BaseResponseDataParse;
import com.wjb.xietong.server.HeaderInfoResponse;
import com.wjb.xietong.server.taskdetail.GetTaskDetailResponse.GetTaskDetailInfo.TaskDetailContentInfo;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;

public class GetTaskDetailResponse extends BaseResponseDataParse {

	private static GetTaskDetailResponse instance = null;

	private HeaderInfoResponse headerInfo = null;
	private List<GetTaskDetailNotifyInfo> getTaskDetailNotifyInfoList = null;
	private List<GetTaskDetailInfo> getTaskDetailInfoList = null;
	private GetTaskDetailHeaderInfo taskHeaderInfo = null;

	private GetTaskDetailResponse() {

	}

	public static synchronized GetTaskDetailResponse instance() {
		if (null == instance) {
			instance = new GetTaskDetailResponse();
		}
		return instance;
	}

	@Override
	public void initData() {
		setHeaderInfo(null);
		setGetTaskInfoList(null);
		setTaskHeaderInfo(null);
	}

	@Override
	public boolean parseJsonObj(JSONObject response) {
		initData();

		if (null == response) {
			LogD.output("Exception: response is null");
			return false;
		}

		if (!checkRespCode(response)) {
			headerInfo = new HeaderInfoResponse();
			headerInfo.parseJsonObj(response);
			setHeaderInfo(headerInfo);
			return false;
		}

		if (response.has(IDs.TASK_MAP)) {
			JSONArray jsonArray = response.optJSONArray(IDs.TASK_MAP);
			if (null != jsonArray && jsonArray.length() > 0) {
				JSONObject jsonObj = jsonArray.optJSONObject(0);
				if (null != jsonObj) {
					taskHeaderInfo = new GetTaskDetailHeaderInfo();
					if (jsonObj.has(IDs.TITLE)) {
						taskHeaderInfo.setTitle(jsonObj.optString(IDs.TITLE));
					}
					if (jsonObj.has(IDs.PROJECT_TITLE)) {
						taskHeaderInfo.setProjectTitle(jsonObj
								.optString(IDs.PROJECT_TITLE));
					}
					if (jsonObj.has(IDs.USER_NAME)) {
						taskHeaderInfo.setUsername(jsonObj
								.optString(IDs.USER_NAME));
					}
					if (jsonObj.has(IDs.OPERATOR_ID)) {
						taskHeaderInfo.setOperatorId(jsonObj
								.optLong(IDs.OPERATOR_ID));
					}
					if (jsonObj.has(IDs.DUEDATE)) {
						taskHeaderInfo.setDueDate(jsonObj
								.optString(IDs.DUEDATE));
					}
					setTaskHeaderInfo(taskHeaderInfo);
				}
			}
		}

		if (response.has(IDs.USER_EXTRACTED_INFO_MAP_LIST)) {
			JSONArray array = response
					.optJSONArray(IDs.USER_EXTRACTED_INFO_MAP_LIST);
			if (null != array) {
				getTaskDetailNotifyInfoList = new ArrayList<GetTaskDetailResponse.GetTaskDetailNotifyInfo>();
				for (int i = 0; i < array.length(); i++) {
					JSONObject jsonObj = array.optJSONObject(i);
					if (null == jsonObj) {
						continue;
					}
					GetTaskDetailNotifyInfo notifyInfo = new GetTaskDetailNotifyInfo();
					if (jsonObj.has(IDs.USER_NAME)) {
						notifyInfo
								.setUsername(jsonObj.optString(IDs.USER_NAME));
					}
					if (jsonObj.has(IDs.USER_ID)) {
						notifyInfo.setUserId(jsonObj.optLong(IDs.USER_ID));
					}
					getTaskDetailNotifyInfoList.add(notifyInfo);
				}
			}
		}

		if (response.has(IDs.REPLY_MAP_LIST)) {
			JSONArray array = response.optJSONArray(IDs.REPLY_MAP_LIST);
			if (null == array) {
				LogD.output("Warning: No Data!");
				return true;
			}

			getTaskDetailInfoList = new ArrayList<GetTaskDetailResponse.GetTaskDetailInfo>();
			for (int i = 0; i < array.length(); i++) {
				JSONObject jsonObj = array.optJSONObject(i);

				if (null == jsonObj) {
					continue;
				}
				GetTaskDetailInfo taskDetailInfo = new GetTaskDetailInfo();
				if (jsonObj.has(IDs.CONTENT)) {
					JSONArray jsonArray = jsonObj.optJSONArray(IDs.CONTENT);
					List<TaskDetailContentInfo> contentInfoList = new ArrayList<TaskDetailContentInfo>();

					if (null != jsonArray) {
						for (int k = 0; k < jsonArray.length(); k++) {
							JSONObject obj = jsonArray.optJSONObject(k);
							TaskDetailContentInfo contentInfo = new TaskDetailContentInfo();
							if (obj.has(IDs.TYPE)) {
								contentInfo.setType(obj.optString(IDs.TYPE));
								if (IDs.CONTENT_TYPE_TEXT.equals(contentInfo
										.getType())) {
									if (obj.has(IDs.TEXT)) {
										contentInfo.setText(obj
												.optString(IDs.TEXT));
									}
								} else if (IDs.CONTENT_TYPE_IMAGE
										.equals(contentInfo.getType())) {
									if (obj.has(IDs.IMAGE_HEIGHT)) {
										contentInfo.setImageHeight(obj
												.optInt(IDs.IMAGE_HEIGHT));
									}
									if (obj.has(IDs.IMAGE_WIDTH)) {
										contentInfo.setImageWidth(obj
												.optInt(IDs.IMAGE_WIDTH));
									}
									if (obj.has(IDs.IMAGE_LINK)) {
										contentInfo.setImageLink(obj
												.optString(IDs.IMAGE_LINK));
									}
								}
							}
							contentInfoList.add(contentInfo);
						}
						taskDetailInfo.setContentInfoList(contentInfoList);
					}
				}

				if (jsonObj.has(IDs.GMT_CREATE1)) {
					taskDetailInfo.setGmt_create(jsonObj
							.optString(IDs.GMT_CREATE1));
				}
				if (jsonObj.has(IDs.CREATE_USER_NAME)) {
					taskDetailInfo.setCreate_username(jsonObj
							.optString(IDs.CREATE_USER_NAME));
				}
				if (jsonObj.has(IDs.DELETED_FLAG)) {
					taskDetailInfo.setDeleted_flag(jsonObj
							.optInt(IDs.DELETED_FLAG));
				}
				if (jsonObj.has(IDs.CREATE_BY_USER_ID)) {
					taskDetailInfo.setCreate_by_user_id(jsonObj
							.optLong(IDs.CREATE_BY_USER_ID));
				}
				if (jsonObj.has(IDs.TASK_ID_2)) {
					taskDetailInfo.setTask_id(jsonObj.optLong(IDs.TASK_ID_2));
				}
				if (jsonObj.has(IDs.GMT_MODIFIED)) {
					taskDetailInfo.setGmt_modified(jsonObj
							.optString(IDs.GMT_MODIFIED));
				}
				if (jsonObj.has(IDs.REPLY_ID)) {
					taskDetailInfo.setReplyId(jsonObj.optLong(IDs.REPLY_ID));
				}
				if (jsonObj.has(IDs.REPLY_TYPE)) {
					taskDetailInfo
							.setReply_type(jsonObj.optInt(IDs.REPLY_TYPE));
				}
				getTaskDetailInfoList.add(taskDetailInfo);
			}
			setGetTaskInfoList(getTaskDetailInfoList);
		}
		return true;
	}

	public static class GetTaskDetailNotifyInfo {
		private long userId = 0l;
		private String username = null;

		public long getUserId() {
			return userId;
		}

		public void setUserId(long userId) {
			this.userId = userId;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

	}

	public static class GetTaskDetailHeaderInfo {
		private String title = null;
		private String username = null;
		private String projectTitle = null;
		private long operatorId = 0l;
		private String dueDate = null;

		public String getProjectTitle() {
			return projectTitle;
		}

		public void setProjectTitle(String projectTitle) {
			this.projectTitle = projectTitle;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public long getOperatorId() {
			return operatorId;
		}

		public void setOperatorId(long operatorId) {
			this.operatorId = operatorId;
		}

		public String getDueDate() {
			return dueDate;
		}

		public void setDueDate(String dueDate) {
			this.dueDate = dueDate;
		}
	}

	public static class GetTaskDetailInfo {

		private String gmt_create = null;
		private String create_username = null;
		private int deleted_flag = 0;
		private long create_by_user_id = 0l;
		private long task_id = 0l;
		private String gmt_modified = null;
		private long replyId = 0l;
		private int reply_type = 0;
		private List<TaskDetailContentInfo> contentInfoList = null;

		public static class TaskDetailContentInfo {
			private String text = null;
			private String type = null;
			private String imageLink = null;
			private int imageHeight = 0;
			private int imageWidth = 0;

			public String getText() {
				return text;
			}

			public void setText(String text) {
				this.text = text;
			}

			public String getType() {
				return type;
			}

			public void setType(String type) {
				this.type = type;
			}

			public String getImageLink() {
				return imageLink;
			}

			public void setImageLink(String imageLink) {
				this.imageLink = imageLink;
			}

			public int getImageHeight() {
				return imageHeight;
			}

			public void setImageHeight(int imageHeight) {
				this.imageHeight = imageHeight;
			}

			public int getImageWidth() {
				return imageWidth;
			}

			public void setImageWidth(int imageWidth) {
				this.imageWidth = imageWidth;
			}
		}

		public List<TaskDetailContentInfo> getContentInfoList() {
			return contentInfoList;
		}

		public void setContentInfoList(
				List<TaskDetailContentInfo> contentInfoList) {
			this.contentInfoList = contentInfoList;
		}

		public String getGmt_create() {
			return gmt_create;
		}

		public void setGmt_create(String gmt_create) {
			this.gmt_create = gmt_create;
		}

		public String getCreate_username() {
			return create_username;
		}

		public void setCreate_username(String create_username) {
			this.create_username = create_username;
		}

		public int getDeleted_flag() {
			return deleted_flag;
		}

		public void setDeleted_flag(int deleted_flag) {
			this.deleted_flag = deleted_flag;
		}

		public long getCreate_by_user_id() {
			return create_by_user_id;
		}

		public void setCreate_by_user_id(long create_by_user_id) {
			this.create_by_user_id = create_by_user_id;
		}

		public long getTask_id() {
			return task_id;
		}

		public void setTask_id(long task_id) {
			this.task_id = task_id;
		}

		public String getGmt_modified() {
			return gmt_modified;
		}

		public void setGmt_modified(String gmt_modified) {
			this.gmt_modified = gmt_modified;
		}

		public long getReplyId() {
			return replyId;
		}

		public void setReplyId(long replyId) {
			this.replyId = replyId;
		}

		public int getReply_type() {
			return reply_type;
		}

		public void setReply_type(int reply_type) {
			this.reply_type = reply_type;
		}
	}

	public HeaderInfoResponse getHeaderInfo() {
		return headerInfo;
	}

	public void setHeaderInfo(HeaderInfoResponse headerInfo) {
		this.headerInfo = headerInfo;
	}

	public List<GetTaskDetailInfo> getGetTaskInfoList() {
		return getTaskDetailInfoList;
	}

	public void setGetTaskInfoList(List<GetTaskDetailInfo> getTaskInfoList) {
		this.getTaskDetailInfoList = getTaskInfoList;
	}

	public GetTaskDetailHeaderInfo getTaskHeaderInfo() {
		return taskHeaderInfo;
	}

	public void setTaskHeaderInfo(GetTaskDetailHeaderInfo taskHeaderInfo) {
		this.taskHeaderInfo = taskHeaderInfo;
	}

	public List<GetTaskDetailNotifyInfo> getGetTaskDetailNotifyInfoList() {
		return getTaskDetailNotifyInfoList;
	}

	public void setGetTaskDetailNotifyInfoList(
			List<GetTaskDetailNotifyInfo> getTaskDetailNotifyInfoList) {
		this.getTaskDetailNotifyInfoList = getTaskDetailNotifyInfoList;
	}
}
